package com.ssyx.service.impl;

import com.ssyx.activity.ActivityFeignClient;
import com.ssyx.product.ProductFeignClient;
import com.ssyx.search.SkuFeignClient;
import com.ssyx.service.ItemService;
import com.ssyx.vo.product.SkuInfoVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ItemServiceImpl implements ItemService {
	
	@Resource
	private ProductFeignClient productFeignClient;
	@Resource
	private ActivityFeignClient activityFeignClient;
	@Resource
	private SkuFeignClient skuFeignClient;
	@Resource
	private ThreadPoolExecutor threadPoolExecutor;
	
	//详情
	@Override
	public Map<String, Object> item(Long skuId, Long userId) {
		Map<String, Object> result = new HashMap<>();
		
		//skuId查询
		CompletableFuture<SkuInfoVo> skuInfocompletableFuture =
				CompletableFuture.supplyAsync(() -> {
					//远程调用获取sku对应数据
					SkuInfoVo skuInfoVo = productFeignClient.getSkuInfoVo(skuId);
					result.put("skuInfoVo", skuInfoVo);
					return skuInfoVo;
				}, threadPoolExecutor);
		
		//sku对应优惠卷信息
		CompletableFuture<Void> activityCompletableFuture = CompletableFuture.runAsync(() -> {
			//远程调用获取优惠卷
			Map<String, Object> activityMap =
					activityFeignClient.findActivityAndCoupon(skuId, userId);
			result.putAll(activityMap);
		}, threadPoolExecutor);
		
		//更新商品热度
		CompletableFuture<Void> hotCompletableFuture = CompletableFuture.runAsync(() -> {
			//远程调用更新热度
			skuFeignClient.incrHotScore(skuId);
		}, threadPoolExecutor);
		
		//任务组合
		CompletableFuture.allOf(
				skuInfocompletableFuture,
				activityCompletableFuture,
				hotCompletableFuture
		).join();
		return result;
	}
}
